package org.qf.web;

import org.qf.pojo.Goods;
import org.qf.service.impl.SmbmsGoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author zzzyyystart
 * @create 2022-05-12-22:35
 */
@WebServlet(value = "/goodsAddServlet")
public class SmbmsGoodsAddServlet extends HttpServlet {
    SmbmsGoodsServiceImpl goodsService = new SmbmsGoodsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //        获取从网页发送过来的属性
        String classify = req.getParameter("classify");
        String goodsName = req.getParameter("goodsName");
        String goodsPrice = req.getParameter("goodsPrice");
        String goodsStock = req.getParameter("goodsStock");
        String imgPath = req.getParameter("imgPath");
        Goods goods = new Goods(null, goodsName, new BigDecimal(goodsPrice),
                0, Integer.valueOf(goodsStock), imgPath);
        req.getSession().setAttribute("classifyName",classify);
        int i = goodsService.addGoods(classify, goods);
        if (i > 0) {
            resp.sendRedirect("goodsServlet?classify="+classify);
        }
        else{
            req.getRequestDispatcher("goodsAdd.jsp").forward(req,resp);
        }

    }
}
